<template>
  <div class="search">
    <div class="search-box-wrapper">
      <!--搜索组件-->
      <SearchBox ref="searchBox" @query="onQueryChange"></SearchBox>
    </div>
    <!--热门搜索数据-->
    <div class="shortcut-wrapper" v-show="!query" ref="shortcutWrapper">
      <scroll :refreshDelay="refreshDelay" class="shortcut" :data="shortcut" ref="shortcut">
        <div>
          <div class="hot-key">
            <h1 class="title">热门搜索</h1>
            <ul>
              <li @click="addQuery(item.k)" class="item" v-for="item in hotKey">
                <span>{{item.k}}</span>
              </li>
            </ul>
          </div>
          <!--搜索历史列表-->
          <div class="search-history" v-show="searchHistory.length">
            <h1 class="title">
              <span class="text">搜索历史</span>
              <span class="clear" @click="showConfirm">
              <i class="icon-clear"></i>
            </span>
            </h1>
            <!--传给子元素-->
            <!--监听子组件的事件-->
            <search-list @select="addQuery" @delete="deleteSearchHistory" :searches="searchHistory"></search-list>
          </div>
        </div>
      </scroll>
    </div>
    <!--搜索的结果-->
    <div class="search-result" v-show="query" ref="searchResult">
      <Suggest ref="suggest" @select="saveSearch" @listScroll="blurInput" :query="query"></Suggest>
    </div>
    <!--取消按钮可以不监听，关闭弹窗即可-->
    <confirm ref="confirm"
      text="是否清空所有搜索历史？"
      confirmBtnText="清空"
      cancelBtnText="取消"
      @confirm="clearSearchHistory"
    ></confirm>
    <!--添加二级路由-->
    <router-view></router-view>
  </div>
</template>

<script type="text/ecmascript-6">
  // 引入搜索框组件
  import SearchBox from '../../base/search-box/search-box'
  // 引入抓取的热门数据
  import {getHotKey} from '../../api/search'
  import {ERR_OK} from '../../api/config'
  // 引入搜索数据
  import Suggest from '../../components/suggest/suggest'
  // 需要vuex
  import {mapActions} from 'vuex'
  // 引入搜索列表
  import SearchList from '../../base/search-list/search-list'
  // 引入弹窗组件
  import confirm from '../../base/confirm/confirm'
  // 引入scroll 组件、
  import scroll from '../../base/scroll/scroll'
  // 修正bottom
  import {playlistMixin, searchMixin} from '../../common/js/mixin'
  // 引入公共部分

  export default {
    // 修正bottom
    mixins: [playlistMixin, searchMixin],
    data() {
      return {
        hotKey: []
      }
    },
    created() {
      this._getHotKey()
    },
    // 添加搜索的内容
    computed: {
      // 计算滚动高度
      shortcut() {
        return this.hotKey.concat(this.searchHistory)
      }
      // ...mapGetters([
      //   'searchHistory'
      // ])
    },
    methods: {
      // 类似ajax得到相关的数据
      _getHotKey() {
        getHotKey().then((res) => {
          if (res.code === ERR_OK) {
            // 截取前十条数据
            this.hotKey = res.data.hotkey.slice(0, 10)
          }
        })
      },
    // //  添加歌曲到搜索框
    //   addQuery(query) {
    //     // 调用子组件的setQuery方法将点击之后的query设置到input框中
    //     this.$refs.searchBox.setQuery(query)
    //   },
    //   // 点击之后将新的query复制给之前的query
    //   onQueryChange(query) {
    //     this.query = query
    //   },
    //   // 将search-box中的input blur调
    //   blurInput() {
    //     // 调用子组件的blur方法
    //     this.$refs.searchBox.blur()
    //   },
    //   // 保存搜索数据结果
    //   saveSearch() {
    //     this.saveSearchHistory(this.query)
    //   },
      // 修正bottom
      handlePlaylist(playlist) {
        const bottom = playlist.length > 0 ? '60px' : ''
        this.$refs.shortcutWrapper.style.bottom = bottom
        this.$refs.searchResult.style.bottom = bottom
        this.$refs.shortcut.refresh()
        // 调用子组件的refresh方法
        this.$refs.suggest.refresh()
      },
      // // 删除搜索结果中的单条数据
      // deleteOne(item) {
      //   this.deleteSearchHistory(item)
      // },
      // // 删除所有的搜索结果
      // deleteAll() {
      //   this.clearSearchHistory()
      // },
      // 显示弹窗
      showConfirm() {
        this.$refs.confirm.show()
      },
      ...mapActions([
        // // 保存搜索数据的结果
        // 'saveSearchHistory',
        // // 删除单条搜索的数据
        // 'deleteSearchHistory'
        // 清空所有的搜索数据
        'clearSearchHistory'
      ])
    },
    watch: {
      query(newQuery) {
        if (!newQuery) {
          setTimeout(() => {
            this.$refs.shortcut.refresh()
          }, 20)
        }
      }
    },
    components: {
      SearchBox,
      Suggest,
      SearchList,
      confirm,
      scroll
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"
  @import "~common/stylus/mixin"

  .search
    .search-box-wrapper
      margin: 20px
    .shortcut-wrapper
      position: fixed
      top: 178px
      bottom: 0
      width: 100%
      .shortcut
        height: 100%
        overflow: hidden
        .hot-key
          margin: 0 20px 20px 20px
          .title
            margin-bottom: 20px
            font-size: $font-size-medium
            color: $color-text-l
          .item
            display: inline-block
            padding: 5px 10px
            margin: 0 20px 10px 0
            border-radius: 6px
            background: $color-highlight-background
            font-size: $font-size-medium
            color: $color-text-d
        .search-history
          position: relative
          margin: 0 20px
          .title
            display: flex
            align-items: center
            height: 40px
            font-size: $font-size-medium
            color: $color-text-l
            .text
              flex: 1
            .clear
              extend-click()
              .icon-clear
                font-size: $font-size-medium
                color: $color-text-d
    .search-result
      position: fixed
      width: 100%
      top: 178px
      bottom: 0
</style>
